package com.applancer.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.applancer.model.CommentModel;
import com.applancer.model.GeneralModel;
import com.applancer.nativecmsflip.R;

public class CommentAdapter extends ArrayAdapter<GeneralModel> {

	protected List<GeneralModel> ldata;

	public CommentAdapter(Context context, List<GeneralModel> objects) {
		super(context, R.layout.adapter_comment, objects);

		ldata = objects;
	}

	@Override
	public int getCount() {
		return ldata.size();
	}

	@Override
	public GeneralModel getItem(int position) {
		return ldata.get(position);
	}

	@Override
	public long getItemId(int position) {
		CommentModel cm = (CommentModel) ldata.get(position);
		String index = cm.getId();

		return Integer.parseInt(index);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.adapter_comment, null);
			holder = new ViewHolder();
			holder.tv_user_post = (TextView) convertView
					.findViewById(R.id.comment_user);
			holder.tv_content_comment = (TextView) convertView
					.findViewById(R.id.comment_content);
			holder.tv_date_comment = (TextView) convertView
					.findViewById(R.id.comment_date);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		CommentModel commentModel = (CommentModel) getItem(position);
		holder.tv_user_post.setText(commentModel.getAuthorName());
		holder.tv_content_comment.setText(commentModel.getContent());
		holder.tv_date_comment.setText( commentModel.getDate() );

		return convertView;
	}

	static class ViewHolder {
		TextView tv_user_post;
		TextView tv_content_comment;
		TextView tv_date_comment;
	}
}
